package org.programmers.anonymous.fightrobots.web.controller;

import java.util.HashMap;
import java.util.Map;

import org.programmers.anonymous.fightrobots.controller.DeffenseController;
import org.programmers.anonymous.fightrobots.model.IDeffense;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/deffense")
public class DeffenseWebController implements BeanFactoryAware {

	private DeffenseController deffenseController;

	@Override
	public void setBeanFactory(BeanFactory context) throws BeansException {
		deffenseController = (DeffenseController) context.getBean("deffenseController");
	}

	//Metodo de Bienvenida
	@RequestMapping(method = RequestMethod.GET)
	public String printWelcome(ModelMap model) 
	{
		model.addAttribute("message", "Deffense Configuration");
		return "listDeffense";
	}

	//Metodo para obtener la getDeffenseList
	@RequestMapping(value="/" ,method = RequestMethod.GET)
	public String getDeffenseList(ModelMap model){

			
	     	Map<Integer,String> deffenseMap=new HashMap<Integer,String>();
	     	 
	     	for (IDeffense iDeffense: deffenseController.getAllDeffenses()){
	     		deffenseMap.put(iDeffense.getIdDeffense(),iDeffense.getName());	     		
	     	}
	     	
			model.addAttribute("deffenseMap", deffenseMap);
			
			return "listDeffense";
	}
}
